package com.net.xpay.web.controller.merchant;

import com.google.common.base.Preconditions;
import com.net.common.model.ResultData;
import com.net.common.model.ResultPagedList;
import com.net.xpay.common.domain.SelfServiceAccount;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.manager.SelfServiceAccountManager;
import com.net.xpay.common.model.search.SelfServiceAccountSearchModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import com.net.xpay.web.secutiry.RequestUser;
import com.net.xpay.web.vo.SelfServiceAccountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/merchant/self-service")
@Api(tags = "商户/手工打款明细", description = "商户自助结算")
public class SelfServiceAccountController {
    @Autowired
    private SelfServiceAccountManager selfServiceAccountManager;

    /**
     * 搜索
     */
    @ApiOperation("搜索")
    @PostMapping("/search")
    public ResultPagedList<SelfServiceAccountVo> search(@ApiIgnore @RequestUser User merchant, @RequestBody SelfServiceAccountSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        searchModel.setUserId(merchant.getId());
        List<SelfServiceAccount> list = selfServiceAccountManager.search(searchModel);
        List<SelfServiceAccountVo> vos = BeanCopierUtil.batchCopy(SelfServiceAccount.class, SelfServiceAccountVo.class, list);
        long count = selfServiceAccountManager.searchCount(searchModel);
        return new ResultPagedList<>(vos, count, searchModel);
    }

    @ApiOperation("新增")
    @PostMapping("/do-add")
    public ResultData<SelfServiceAccount> doAdd(@ApiIgnore @RequestUser User merchant, @RequestBody SelfServiceAccount selfServiceAccount) {
        Preconditions.checkArgument(merchant.getId() != null);
        selfServiceAccount.setUserId(merchant.getId());
        SelfServiceAccount record = selfServiceAccountManager.doAdd(selfServiceAccount);
        return new ResultData<>(record);
    }

    @ApiOperation("修改")
    @PostMapping("/do-update")
    public ResultData<SelfServiceAccount> doUpdate(@ApiIgnore @RequestUser User merchant, @RequestBody SelfServiceAccount selfServiceAccount) {
        Preconditions.checkArgument(selfServiceAccount.getId() != null);
        SelfServiceAccount record = selfServiceAccountManager.doUpdate(selfServiceAccount);
        return new ResultData<>(record);
    }

    @ApiOperation("详情")
    @PostMapping("/do-detail")
    public ResultData<SelfServiceAccount> doDetail(@RequestBody SelfServiceAccount selfServiceAccount) {
        Preconditions.checkArgument(selfServiceAccount.getId() != null);
        SelfServiceAccount record = selfServiceAccountManager.doDetail(selfServiceAccount);
        return new ResultData<>(record);
    }
}
